package com.tzy.common.constant;
import com.tzy.common.utils.MessageUtil;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Data
public class Result implements Serializable {
    private ResultCode resultCode;
    //消息代码,0正常,1错误
    private int code;
    //操作消息
    private String msg;
    //数据
//    private Object data;
    private Map<String,Object> result = new HashMap<>();

    public Result() { }

    public Result putData(String key, Object result) {
        this.result.put(key, result);
        return this;
    }
    public Result(ResultCode resultCode, Object data) {
        this.code = resultCode.code();
        this.result.put("data",data);
    }

    public static Result success(){
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }
    //返回成功
    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode (ResultCode.SUCCESS);
        result.result.put("data",data);
        return result;
    }
    //返回失败
    public static Result failure (ResultCode resultCode) {
        Result result = new Result();
        result.msg = MessageUtil.getMsg(result.code);
        result.setResultCode( resultCode ) ;
        return result;
    }
    //返回失败
    public static Result failure(ResultCode resultCode,Object data) {
        Result result = new Result();
        result.msg = MessageUtil.getMsg(result.code);
        result.setResultCode ( resultCode );
        result.result.put("data",data);
        return result;
    }
}
